package com.huiben.app.cartoonbooks.app;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.huiben.app.cartoonbooks.service.PlayerService;
import com.lzy.okgo.OkGo;

/**
 * Created by TwoSX on 2016/6/4.
 */
public class MyApp extends Application {

    private static PlayerService sPlayerService;
    private static boolean isBind = false;
    public  static  MyApp baseApp ;
    public  static MyApp getInst(){
        return  baseApp;
    }
    @Override
    public void onCreate() {
        super.onCreate();
         OkGo.init(this);
        baseApp=this;
        if (!isBind) {
            bindService(new Intent(getApplicationContext(), PlayerService.class), mConnection, BIND_AUTO_CREATE);
        }
    }
    public Context getContext(){
        return  baseApp.getApplicationContext();
    }
    private static ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

                Log.d("MyApp", "服务绑定成功");
            sPlayerService = ((PlayerService.MyBinder) service).getService();
            isBind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 服务被异常终止
            setPlayerService(null);
            isBind = false;
        }

    };

    public static ServiceConnection getmConnection() {
        return mConnection;
    }

    public static void setPlayerService(PlayerService playerService) {
        sPlayerService = playerService;
    }

    public static PlayerService getPlayerService() {
        return sPlayerService;
    }

}
